package com.pignest.hotel.ui.member;

import java.util.List;

import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;

import com.pignest.framework.util.Utils;
import com.pignest.hotel.beans.MemberLevel;
import com.pignest.hotel.beans.MemberPrice;
import com.pignest.hotel.beans.RoomType;
import com.pignest.hotel.service.MemberService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.util.MoneyUtil;
import com.pignest.hotel.util.UIUtils;
import com.pignest.hotel.util.WhereCreator;

public class MemberPriceEditListbox extends Listbox implements EventListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -2840860719936101856L;
	private MemberService memberService;
	
    private String queryLevelId;
	
	private String queryRoomTypeId;
	
	
	public MemberPriceEditListbox(){
		memberService=Services.getMemberService();
		queryEidtList(null,null);
	}
	@Override
	public void onEvent(Event event) throws Exception {
		// TODO Auto-generated method stub
		if ("onCreate".equals(event.getName())) {
			queryEidtList(null,null);
		}

	}
	public void queryEidtList(String roomtype,String memberLevel){

		this.getItems().clear();
		StringBuffer hql=new StringBuffer();
		WhereCreator where =new WhereCreator();
		hql.append(" from MemberPrice b where 1=1 ");
		if(roomtype!=null&&!roomtype.equals("")){
			hql.append(" and b.roomType.id= '"+roomtype+"'");
		}
		if(memberLevel!=null&&!memberLevel.equals("")){
			hql.append(" and b.memberLevel.id = '"+memberLevel+"'");
		}
		List<MemberPrice> memberPrices=memberService.findMemberPrice(hql.toString());
		int i=1;
		for(final MemberPrice p:memberPrices){
			Listitem listitem = new Listitem();
			listitem.setValue(p.getId());
			listitem.setParent(this);
			listitem.appendChild(new Listcell(Integer.toString(i)));
			listitem.appendChild(new Listcell(p.getMemberLevel().getMemberlevel()));
			listitem.appendChild(new Listcell(p.getRoomType().getName()));
			listitem.appendChild(new Listcell(Utils.toPrice(p.getRoomType().getPrice())));
			Listcell cell = new Listcell();
			final Intbox pInbox = new Intbox();
			Integer price =p.getPrice();
			if(price!=null)
				pInbox.setValue(MoneyUtil.rand(MoneyUtil.cent2dollar(price)));
			else
				pInbox.setValue(0);
			cell.appendChild(pInbox);
			listitem.appendChild(cell);
			Button editBtn = new Button();
			Listcell cell1 = new Listcell();
			cell1.appendChild(editBtn);
			editBtn.setLabel("保存");
			listitem.appendChild(cell1);
			editBtn.addEventListener(Events.ON_CLICK, new EventListener(){
				 public void onEvent(Event event) throws Exception { 
					 //System.out.println("&&&&&&&&"+p.getId()+pInbox.getValue());
					 edit(p.getId(),pInbox.getValue());
				 }
			});
			
			i++;
		}
		
	}
	public void edit(String id,int mPrice){
		try{
			MemberPrice p=memberService.getMemberPriceId(id);
			p.setPrice(MoneyUtil.dollar2cent(mPrice));
			memberService.updateMemberPrice(p);
			//queryEidtList(null,null);
			queryPrice();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public void initData(){
		memberService.initMemberPrices();
		queryEidtList(null,null);
	}
	
	public String getQueryLevelId() {
		return queryLevelId;
	}
	public void setQueryLevelId(Listbox levelListbox) {
		String levelId=(String)UIUtils.getSelectedValue(levelListbox);
		this.queryLevelId = levelId;
	}
	public String getQueryRoomTypeId() {
		return queryRoomTypeId;
	}
	public void setQueryRoomTypeId(Listbox roomtypeListbox) {
		String roomtypeId=(String)UIUtils.getSelectedValue(roomtypeListbox);
		this.queryRoomTypeId = roomtypeId;
	}
	public void queryPrice(){
		queryEidtList(this.queryRoomTypeId,this.queryLevelId);
	}


}
